package com.yg.controller;

import com.yg.config.FilePathConfig;
import com.yg.config.result.R;
import com.yg.utils.DownLoadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * 表格模板文件下载
 */
@RestController
@RequestMapping("template")
@CrossOrigin
public class TemplateDownloadController {


    @Autowired
    private FilePathConfig config;

    @GetMapping("download")
    public R downloadTemplate(@RequestParam("template") String file, HttpServletResponse response) {
        String readExcelTemplateFilePath = config.readExcelTemplateFilePath;
        try {
            String res = DownLoadUtil.downLoadFile(response, readExcelTemplateFilePath, file);
            return R.getR().message(res);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return R.ERROR().message("文件下载错误");
        }
    }
}